﻿//////////////////////////////////////////////////////////////////////////
// FileName:fmSecurityTrainingManager.aspx
// CreateBy:DragonXiong
// CreateDate:2010-05-25
// Description:
//		 Security Training Manager
//      
// Modify Log:
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
using BLL = Auto.Data.BusMan.BLL;

namespace Auto.BusMan
{
    public partial class fmSecruityAnalysisMeetingManager : System.Web.UI.Page
    {
        public string DocID
        {
            get
            {
                ViewState["DocID"] = Request.QueryString["DocID"];
                return (ViewState["DocID"] == null) ? "" : ViewState["DocID"].ToString();
            }
            set { ViewState["DocID"] = value; }
        }

        Model.SecruityAnalysisMeeting model = new Model.SecruityAnalysisMeeting();
        BLL.SecruityAnalysisMeeting bll = new BLL.SecruityAnalysisMeeting();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            if (string.IsNullOrEmpty(DocID)) //新增
            {
                this.chkAddContinue.Checked = true;
                checktr.Visible = true;
                checktr.Attributes.Add("style", "display:block;");
            }
            else  //修改
            {
                checktr.Attributes.Add("style", "display:none;");
                InitControl();
            }
        }

        private void ClearText()
        {
            txtMeetingTime.Text = "";
            txtMeetingAddress.Text = "";
            this.txtModerator.Text = "";
            this.txtAttendees.Text = "";
            this.txtMeetingAgenda.Text = "";
            this.txtMeetingMinutes.Text = "";
        }

        private void InitControl()
        {

            model = bll.GetModel(DocID);
            Session["fmSecruityAnalysisMeetingManager.Model"] = model;
            txtMeetingTime.CalendarDate = model.MeetingTime;
            txtMeetingAddress.Text = model.MeetingAddress;
            this.txtModerator.Text = model.Moderator;
            this.txtAttendees.Text = model.Attendees;
            this.txtMeetingAgenda.Text = HtmlDiscode(model.MeetingAgenda);
            this.txtMeetingMinutes.Text = HtmlDiscode(model.MeetingMinutes);
        }
        private Model.SecruityAnalysisMeeting GetSecruityAnalysisMeeting()
        {
            if (!string.IsNullOrEmpty(DocID))
            {
                model = (Model.SecruityAnalysisMeeting)Session["fmSecruityAnalysisMeetingManager.Model"];
            }
            //Tab1
            DateTime MeetingTime = DateTime.Parse(this.txtMeetingTime.Text);
            string MeetingAddress = this.txtMeetingAddress.Text;
            string Moderator = this.txtModerator.Text;
            string Attendees = this.txtAttendees.Text;
            string MeetingAgenda = this.txtMeetingAgenda.Text;
            string MeetingMinutes = this.txtMeetingMinutes.Text;
            //string Note = this.txtNote.Text;

            //Tab1
            model.MeetingTime = MeetingTime;
            model.MeetingAddress = MeetingAddress;
            model.Moderator = Moderator;
            model.Attendees = Attendees;
            model.MeetingAgenda = HtmlEncode(MeetingAgenda);
            model.MeetingMinutes = HtmlEncode(MeetingMinutes);
            //model.Note = Note;

            return model;


        }

        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            model = GetSecruityAnalysisMeeting();

            model.UserAcct = SessionBox.User.UserID;
            try
            {
                if (string.IsNullOrEmpty(DocID))
                {
                    model.RecDivID = SessionBox.User.DivID;
                    bll.Add(model);
                }
                else
                {
                    bll.Update(model);
                }
                Redirect();

            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this, ex.Message);
            }
        }

        private void Redirect()
        {
            if (chkAddContinue.Checked)
            {
                lblMsg.Text = "保存成功，请继续添加！";
                ClearText();
            }
            else
            {
                Server.Transfer("fmSecruityAnalysisMeetingList.aspx");
            }
        }

        #endregion Event

        /// <summary>
        /// 替换html中的特殊字符
        /// </summary>
        /// <param name="theString">需要进行替换的文本。</param>
        /// <returns>替换完的文本。</returns>
        private string HtmlEncode(string theString)
        {
            theString = theString.Replace(">", "&gt;");
            theString = theString.Replace("<", "&lt;");
            theString = theString.Replace("  ", " &nbsp;");
            theString = theString.Replace("  ", " &nbsp;");
            theString = theString.Replace("\"", "&quot;");
            theString = theString.Replace("\'", "&#39;");
            theString = theString.Replace("\n", "<br/> ");
            return theString;
        }

        /// <summary>
        /// 恢复html中的特殊字符
        /// </summary>
        /// <param name="theString">需要恢复的文本。</param>
        /// <returns>恢复好的文本。</returns>
        private string HtmlDiscode(string theString)
        {
            theString = theString.Replace("&gt;", ">");
            theString = theString.Replace("&lt;", "<");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace(" &nbsp;", "  ");
            theString = theString.Replace("&quot;", "\"");
            theString = theString.Replace("&#39;", "\'");
            theString = theString.Replace("<br/> ", "\n");
            return theString;
        }

    }
}